package communicationMessages;


import java.sql.ResultSet;
import java.sql.SQLException;

import logger.LoggerWriter;

import serverComuncation.Observer;

import data.User;
import database.DB;

public abstract class Message {
	

	public static final String delimiter = ((char) 1) + "";
	protected String content;
	protected User user;
	
	
	public static Message parseMsg (String msg){
		int endIndex = msg.indexOf(delimiter);
		String title = msg.substring(0, endIndex);
		//System.out.println("msg is:"+msg);
		Message ans = getMessageType(title);
		ans.setContent(msg.substring(endIndex+1));
		return ans;
	}
	
	public void activateUser(String userName)
	{
		Observer.observer.activateUser(Thread.currentThread().getId(), userName);
	}
	
	public void unActivateUser()
	{
		Observer.observer.unActivateUser(Thread.currentThread().getId());
	}
	
	public String getContent() {
		return content;
	}

	public void setContent(String content) {
		this.content = content;
	}

	public User getUser() {
		return user;
	}

	public void setUser(User user) {
		this.user = user;
	}
	private static Message getMessageType(String title) {
		String register ="register",login = "login",viewForum = "viewForum",viewSubForum ="viewSubForum",viewThread = "viewThread",
				logout = "logout",addThread = "addThread",addPost = "addPost",addSubForum = "addSubForum", 
				removeModerator = "removeModerator",GetModerators = "GetModerators" ,EditMsg = "EditMsg",deleteMsg = "deleteMsg",
				DeleteSubForum ="DeleteSubForum", ModeratorsCandidate = "ModeratorsCandidate", AddModerator = "AddModerator",
				numOfMsgs = "numOfMsgs",msgsByUser = "msgsByUser",msgCount ="msgCount",adminCandidate = "adminCandidate", 
				changeAdmin = "changeAdmin",numOfConnection = "numOfConnection"	,changeActiveMode = "changeActiveMode",
				changedBannedModeState="changedBannedModeState", GetListOfOptionalBanneded = "GetListOfOptionalBannededMessage";
		if (title.equalsIgnoreCase(register))
			return new RegisterMessage();
		else if (title.equalsIgnoreCase(login))
			return new LoginMessage();
		else if (title.equalsIgnoreCase(viewForum))
			return new ViewForumMessage();
		else if (title.equalsIgnoreCase(viewSubForum))
			return new ViewSubForumMessage();
		else if (title.equalsIgnoreCase(viewThread))
			return new ViewThreadMessage();
		else if (title.equalsIgnoreCase(logout))
			return new LogoutMessage();
		else if (title.equalsIgnoreCase(addThread))
			return new AddThreadMessage();
		else if (title.equalsIgnoreCase(addPost))
			return new AddPostMessage();
		else if (title.equalsIgnoreCase(addSubForum))
			return new AddSubForumMessage();
		else if (title.equalsIgnoreCase(removeModerator))
			return new removeModeratorMessage();
		else if (title.equalsIgnoreCase(GetModerators))
			return new GetModeratorMessage();
		else if (title.equalsIgnoreCase(EditMsg))
			return new EditMsgMessage();
		else if (title.equalsIgnoreCase(DeleteSubForum))
			return new DeleteSubForumMessage();
		else if (title.equalsIgnoreCase(ModeratorsCandidate))
			return new CandidatesModeratorsMessage();
		else if (title.equalsIgnoreCase(AddModerator))
			return new AddModeratorMessage();
		else if (title.equalsIgnoreCase(deleteMsg))
			return new DeleteMsgMessage();
		else if (title.equalsIgnoreCase(numOfMsgs))
			return new NumOfMsgsMessage();
		else if (title.equalsIgnoreCase(msgsByUser))
			return new MsgsByUserMessage();
		else if (title.equalsIgnoreCase(msgCount))
			return new MsgCountMessage();
		else if (title.equalsIgnoreCase(adminCandidate))
			return new AdminCandidateMessage();
		else if (title.equalsIgnoreCase(changeAdmin))
			return new ChangeAdminMessage();
		else if (title.equalsIgnoreCase(numOfConnection))
			return new NumOfConnectionMessage();
		else if (title.equalsIgnoreCase(changeActiveMode))
			return new ChangeActiveModeMessage();
		else if (title.equalsIgnoreCase(changedBannedModeState))
			return new ChangedBannedModeStateMessage();
		else if (title.equalsIgnoreCase(GetListOfOptionalBanneded))
			return new GetListOfOptionalBannededMessage();
		
		
		//need to add  a method "viewPost" and to implement
		
		throw new RuntimeException("unknow title to message " + title );
	}


	public abstract String getAnswer();
	
	public String getUsersNameFromResultSet(String answer, ResultSet res) {
		try
		{
			while(res.next())
				answer+= res.getString(DB.userTable.userName.ordinal())+ delimiter;
		}
		catch (SQLException e)
		{
			System.out.println("MsgsByUserMessage SQLException" + e.getMessage());
		}
		return answer;
	}

	public void sendMsgToObserver(String subForumId, String msg,String webMsg) {
		Observer.addNewMsg(addFinshToAns(msg),addFinshToAns(webMsg),Integer.parseInt(subForumId));
	}

	protected void sendRefreshMsg(LoggerWriter logger, String subForumId,int threadId,boolean changePath)
			throws SQLException {
				String refresh = "ThreadsList"+delimiter;
				refresh = ViewSubForumMessage.getViewSubForumMsg(logger, refresh, subForumId, "guest",changePath);
				String webRefresh = "refresh"+ delimiter + "thread" + delimiter + threadId + delimiter;
				Observer.addNewRefreshMsg(addFinshToAns(refresh),addFinshToAns(webRefresh), Integer.parseInt(subForumId),true);
			}
	
	protected void sendRefreshPostMsg(String threadMsgId, int postMsgId) {
		String refresh = "doRefresh" + delimiter;
		String webRefresh = "refresh"+ delimiter + "post" + delimiter + postMsgId + delimiter;
		Observer.addNewRefreshMsg(addFinshToAns(refresh), addFinshToAns(webRefresh), Integer.parseInt(threadMsgId), false);
	}

	protected String getAddThreadPopupMsg(int threadId) {
		String webPopupMsg = "popupMessage" + delimiter + "addedthread" + delimiter + threadId + delimiter;
		return webPopupMsg;
	}

	protected String getAddPostWebPopupMsg(int postMsgId) {
		String webMsg = "popupMessage" + delimiter + "addedpost"+ delimiter + postMsgId +  delimiter ;
		return webMsg;
	}

	protected void changeCurrentPathViewThread(int thread, long id) {
		Observer.observer.changeCurentPathViewThread(id, thread);
		
	}

	protected static void changeCurrentPath(int subForumId, long threadId) {
		Observer.observer.changeCurentPath(threadId, subForumId);
	}

	public static String addFinshToAns(String msg)
	{
		return msg+"\n" + "\0";
	}
	
	protected void addToBannedUsersIfNeeded(String userName,int userId, String subForumId) {
		if (!DB.isActiveUser(userName))
		{
			if (!DB.isRowExistBannedUsers(userId,subForumId))
			{
				DB.addBannedUsers(userId,subForumId);
			}
		}
		
	}
	
	

}
